package com.cqndt.disaster.device.controller;

import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.math.BigDecimal;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;
import com.cqndt.disaster.device.util.AjaxResult;
import org.springframework.web.bind.annotation.PostMapping;
import com.cqndt.disaster.device.service.AppTabDeviceService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;

@RestController
@RequestMapping("/api/v2/device")
@Slf4j
public class DeviceController extends BaseController {
	
    @Autowired
    private AppTabDeviceService appTabDeviceService;
    /**
     * @param id 用户id
     * @return 所有设备在线率
     **/
    @PostMapping(value="selectOnlineDevice")
    public AjaxResult OnlineRate(HttpServletRequest request) {
        String userId = request.getHeader("id");
        //设备总数
        Integer countDevice = appTabDeviceService.selectCountDevice(userId);
        //设备在线数量
        Integer onlineCountDevice = appTabDeviceService.selectOnlineCountDevice(userId);
        //设备离线数量
        Integer offLineCountDevice = countDevice-onlineCountDevice;
        double q = 0d;
        try {
            q = new BigDecimal((float) onlineCountDevice / countDevice).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        } catch (ArithmeticException e) {
            System.out.println("除数不能为0");
        }
        String OnlineRate = String.valueOf(q * 100);
        Map<String,String> map = new HashMap<>(4);
        map.put("onlineRate",String.valueOf(OnlineRate));
        map.put("countDevice",String.valueOf(countDevice));
        map.put("onlineCountDevice",String.valueOf(onlineCountDevice));
        map.put("offLineCountDevice",String.valueOf(offLineCountDevice));
        return entityResult(map, "获取成功");
    }

    /**
     * @param request
     * @param seachText 项目名称
     * @return 查询每个项目所有设备在线情况
     **/
    @PostMapping("selectOnlineRateByProject")
    public AjaxResult selectOnlineRateByProject(HttpServletRequest request,String seachText) {
        String userId = request.getHeader("id");
        List<Map<String, Object>> mapList = appTabDeviceService.selectOnlineRateByProject(userId,seachText);
        for (Map<String, Object> map : mapList) {
            int totalNum = Integer.parseInt(map.get("totalNum").toString());
            int zx = Integer.parseInt(map.get("zx").toString());
            double q = 0d;
            try {
                if(totalNum !=0){
                    q = new BigDecimal((float) zx / totalNum).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                }
            } catch (ArithmeticException e) {
                System.out.println("除数不能为0");
            }
            String OnlineRate = String.valueOf(q * 100);
            map.put("OnlineRate", OnlineRate);
        }
        return entityResult(mapList, "获取成功");
    }

    /**
     * @param ProjectID 项目id
     * @return 查询某个项目下的设备情况及负责人
     **/
    @PostMapping("selectOnlineDeviceByProjectID")
    public AjaxResult selectOnlineDeviceByProjectID(Integer ProjectID) {
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> mapList = appTabDeviceService.selectOnlineDeviceByProjectID(ProjectID);
        List<Map<String, Object>> person = appTabDeviceService.selectPerson(ProjectID);
        log.info("查询的人"+person);
        if (!person.isEmpty()){
            map.put("person", person.get(0));
        }else {
            map.put("person", null);
        }
        map.put("ListDevice", mapList);
        return entityResult(map, "获取");
    }

    /**
     * @param deviceId 设备id
     * @return 根据设备id查询所在项目的监测点
     **/
    @PostMapping("deviceCoordinateAll")
    public AjaxResult deviceCoordinateAll(Integer deviceId) {
        Map<String, Object> deviceInfo = appTabDeviceService.selectDeviceById(deviceId);
        if (deviceInfo.containsKey("id")) {
            int id = (int) deviceInfo.get("id");
            return entityResult(appTabDeviceService.deviceCoordinateByProjectId(id), "获取成功");
        }else {
            return fallback();
        }
    }

    /**
     * @param deviceId 设备id
     * @param sensorType 传感器类型
     * @param sensorNo 传感器编号
     * @return 根据设备id查询设备信息
     **/
    @PostMapping("selectDeviceById")
    public AjaxResult selectDeviceById(Integer deviceId,String sensorType,String sensorNo) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> deviceInfo = appTabDeviceService.selectDeviceById(deviceId);
        map.put("deviceInfo", deviceInfo);
        if (null != deviceInfo) {
            map.put("monitor", appTabDeviceService.monitorStatistics(sensorType,"week",sensorNo));
        }
        return entityResult(map, "获取成功");
    }
}